<template>
  <div class="confirmOrder">
    <!-- 头部 -->
    <div class="confirmOrder-header">
      <div class="header-content">
        <p>
          <i class="el-icon-s-order"></i>
        </p>
        <p>确认订单</p>
        <router-link to></router-link>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容容器 -->
    <div class="content">
      <!-- 选择地址 -->
      <div class="section-address">
        <h2>收货地址</h2>
        <div class="address-body">
          <ul>
            <li
                    :class="item.id == confirmAddress ? 'in-section' : ''"
                    v-for="item in address"
                    :key="item.id"
            >
              <h2>{{item.name}}</h2>
              <p class="phone">{{item.phone}}</p>
              <p class="address">{{item.address}}</p>
            </li>
            <li class="add-address">
              <i class="el-icon-circle-plus-outline"></i>
              <p>添加新地址</p>
            </li>
          </ul>
        </div>
      </div>
      <!-- 选择地址END -->

      <!-- 商品 -->
      <div class="section-goods">
        <p class="title">结算列表</p>
        <div class="goods-list">
          <ul>
            <li v-for="item in getCheckGoods" :key="item.id">
              <img :src="$target + item.productImg" />
              <span class="pro-name">{{item.productName}}</span>
              <span class="pro-price">{{item.price}}元 x {{item.num}}</span>
              <span class="pro-status"></span>
              <span class="pro-total">{{item.price * item.num}}元</span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 商品END -->


      <!-- 结算列表 -->
      <div class="section-count">
        <div class="money-box">
          <ul>
            <li>
              <span class="title">商品件数：</span>
              <span class="value">{{getCheckNum}}件</span>
            </li>
            <li>
              <span class="title">商品总价：</span>
              <span class="value">{{getTotalPrice}}元</span>
            </li>
            <li class="total">
              <span class="title">应付总额：</span>
              <span class="value">
                <span class="total-price">{{getTotalPrice}}</span>元
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 结算列表END -->

      <!-- 结算导航 -->
      <div class="section-bar">
        <div class="btn">
          <router-link to="/shoppingCart" class="btn-base btn-return">返回购物车</router-link>
          <a href="javascript:void(0);" @click="addOrder" class="btn-base btn-primary">结算</a>
        </div>
      </div>
      <!-- 结算导航END -->
    </div>
    <!-- 主要内容容器END -->
  </div>
</template>



<script>
  import { mapGetters } from "vuex";
  import { mapActions } from "vuex";
  export default {
    name: "",
    data() {
      return {
        // 虚拟数据
        confirmAddress: 1, // 选择的地址id
        // 地址列表
        address: [
          {
            id: 1,
            name: "李雷",
            phone: "123456",
            address: "天津的学校"
          },
          {
            id: 2,
            name: "韩梅梅",
            phone: "654321",
            address: "李雷的隔壁"
          }
        ]
      };
    },
    created() {
      // 如果没有勾选购物车商品直接进入确认订单页面,提示信息并返回购物车
      if (this.getCheckNum < 1) {
        this.notifyError("请勾选商品后再结算");
        this.$router.push({ path: "/shoppingCart" });
      }
    },
    computed: {
      // 结算的商品数量; 结算商品总计; 结算商品信息
      ...mapGetters(["getCheckNum", "getTotalPrice", "getCheckGoods"])
    },
    methods: {
      ...mapActions(["deleteShoppingCart"]),
      addOrder() {
        this.$axios
                .post("/api/user/order/addOrder", {
                  user_id: this.$store.getters.getUser.user_id,
                  products: this.getCheckGoods
                })
                .then(res => {
                  let products = this.getCheckGoods;
                  switch (res.data.code) {
                          // “001”代表结算成功
                    case "001":
                      for (let i = 0; i < products.length; i++) {
                        const temp = products[i];
                        // 删除已经结算的购物车商品
                        this.deleteShoppingCart(temp.id);
                      }
                      // 提示结算结果
                      this.notifySucceed(res.data.msg);
                      // 跳转我的订单页面
                      this.$router.push({ path: "/order" });
                      break;
                    default:
                      // 提示失败信息
                      this.notifyError(res.data.msg);
                  }
                })
                .catch(err => {
                  return Promise.reject(err);
                });
      }
    }
  };
</script>



<style scoped>
  .confirmOrder {
    background-color: whitesmoke;
    padding-bottom: 20px;
  }
  /* 头部CSS */
  .confirmOrder .confirmOrder-header {
    background-color: #fff;
    border-bottom: 2px solid orangered;
    margin-bottom: 20px;
  }
  .confirmOrder .confirmOrder-header .header-content {
    width: 1225px;
    margin: 0 auto;
    height: 80px;
  }
  .confirmOrder .confirmOrder-header .header-content p {
    float: left;
    font-size: 28px;
    line-height: 80px;
    color: #424242;
    margin-right: 20px;
  }
  .confirmOrder .confirmOrder-header .header-content p i {
    font-size: 45px;
    color: orangered;
    line-height: 80px;
  }
  /* 头部CSS END */

  /* 主要内容容器CSS */
  .confirmOrder .content {
    width: 1225px;
    margin: 0 auto;
    padding: 48px 0 0;
    background-color: #fff;
  }

  /* 地址CSS */
  /*这是地址栏的整体*/
  .confirmOrder .content .section-address {
    margin: 0 45px;
    overflow: hidden;
  }
  .confirmOrder .content .section-address .title {
    color: darkgray;
    font-size: 18px;
    line-height: 20px;
    margin-bottom: 20px;
  }

  .confirmOrder .content .address-body li {
    float: left;
    color: #333;
    width: 220px;
    height: 178px;
    border: 1px solid #e0e0e0;
    padding: 15px 24px 0;
    margin-right: 17px;
    margin-bottom: 24px;
  }

  .confirmOrder .content .address-body .in-section {
    border: 1.5px solid orangered;
  }
  .confirmOrder .content .address-body li h2 {
    font-size: 18px;
    font-weight: normal;
    line-height: 30px;
    margin-bottom: 10px;
  }
  .confirmOrder .content .address-body li p {
    font-size: 14px;
    color: dimgray;
  }
  .confirmOrder .content .address-body li .address {
    padding: 10px 0;
    max-width: 180px;
    max-height: 80px;
    line-height: 22px;
    overflow: hidden;
  }
  .confirmOrder .content .address-body .add-address {
    text-align: center;
    line-height: 30px;
  }
  .confirmOrder .content .address-body .add-address i {
    font-size: 30px;
    padding-top: 50px;
    text-align: center;
  }
  /* 地址CSS END */

  /* 商品CSS */
  .confirmOrder .content .section-goods {
    margin: 0 48px;
  }
  .confirmOrder .content .section-goods p.title {
    color: black;
    font-size: 18px;
    line-height: 40px;
  }
  .confirmOrder .content .section-goods .goods-list {
    padding: 5px 0;
    border-top: 1px solid #e0e0e0;
    border-bottom: 1px solid #e0e0e0;
  }
  .confirmOrder .content .section-goods .goods-list li {
    padding: 10px 0;
    color: #424242;
    overflow: hidden;
  }
  .confirmOrder .content .section-goods .goods-list li img {
    float: left;
    width: 30px;
    height: 30px;
    margin-right: 10px;
  }
  .confirmOrder .content .section-goods .goods-list li .pro-name {
    float: left;
    width: 650px;
    line-height: 30px;
  }
  .confirmOrder .content .section-goods .goods-list li .pro-price {
    float: left;
    width: 150px;
    text-align: left;
    line-height: 30px;
  }
  .confirmOrder .content .section-goods .goods-list li .pro-total {
    float: left;
    width: 190px;
    text-align: right;
    color: orangered;
    line-height: 28px;
  }
  /* 商品CSS END */



  /* 结算列表CSS */
  .confirmOrder .content .section-count {
    margin: 0 48px;
    padding: 20px 0;
    overflow: hidden;
  }
  .confirmOrder .content .section-count .money-box {
    float: right;
    text-align: right;
  }
  .confirmOrder .content .section-count .money-box .title {
    float: left;
    width: 126px;
    height: 30px;
    display: block;
    line-height: 30px;
    color: #757575;
  }
  .confirmOrder .content .section-count .money-box .value {
    float: left;
    min-width: 105px;
    height: 30px;
    display: block;
    line-height: 30px;
    color: orangered;
  }
  .confirmOrder .content .section-count .money-box .total .title {
    padding-top: 15px;
  }
  .confirmOrder .content .section-count .money-box .total .value {
    padding-top: 10px;
  }
  .confirmOrder .content .section-count .money-box .total-price {
    font-size: 30px;
  }
  /* 结算列表CSS END */

  /* 结算导航CSS */
  .confirmOrder .content .section-bar {
    padding: 20px 48px;
    border-top: 2px solid #f5f5f5;
    overflow: hidden;
  }
  .confirmOrder .content .section-bar .btn {
    float: right;
  }
  .confirmOrder .content .section-bar .btn .btn-base {
    float: left;
    margin-left: 30px;
    width: 158px;
    height: 38px;
    border: 1px solid #b0b0b0;
    font-size: 14px;
    line-height: 38px;
    text-align: center;
  }
  .confirmOrder .content .section-bar .btn .btn-return {
    color: rgba(0, 0, 0, 0.27);
    border-color: rgba(0, 0, 0, 0.27);
  }
  .confirmOrder .content .section-bar .btn .btn-primary {
    background: orangered;
    border-color: orangered;
    color: #fff;
  }
  /* 结算导航CSS */

  /* 主要内容容器CSS END */
</style>